using System;
using System.Data;
using System.Diagnostics;
using System.IO;
using PHSRAG.Insight21.Common.Pages;
using PHSRAG.Utility;

namespace PHSRAG.Insight21.Common
{
    /// <summary>
    /// The Help page is a central mechanism in displaying on-line help.
    /// As of this writing, no on-line content has been identified to fall within the help category.
    /// In addition to the on-line help, the current version number of all assemblies built as part of the Insight
    /// application is displayed.
    /// </summary>
    public partial class HelpDesk : BasePage
    {
        #region Constants

        private const string AssemblyNameColumn = "AssemblyName";
        private const string AssemblyVersionColumn = "AssemblyVersion";
        private const string BuildTimestampColumn = "BuildTimestamp";

        #endregion

        #region Event Handlers

        /// <summary>
        /// Load the version numbers of all assemblies into the data grid.
        /// </summary>
        /// <param name="sender">Control which raised the event (this page)</param>
        /// <param name="e">Argument associated with the event</param>
        protected void OnPageLoad(object sender, EventArgs e)
        {
            //enableviewstate = false;

            //if (!ispostback)
            //{
            //    dataaccess.connection connection = dataaccess.getconnection();
            //    databaseconnection.text = string.format("database server: {0}, catalog: {1}, userid: {2}",
            //        connection.server, connection.catalog, connection.userid);

            //    datatable table = new datatable();
            //    table.columns.add(assemblynamecolumn);
            //    table.columns.add(assemblyversioncolumn);
            //    table.columns.add(buildtimestampcolumn);

            //    loadassemblyversioninformation(table);
            //    loaddatabaseversioninformation(table);

            //    versioninfo.datasource = table;
            //    versioninfo.databind();
            //}
        }

        #endregion

        #region Private Methods

        /// <summary>
        /// Version information is retrieved for all assemblies within the bin folder of the application. This infomration
        /// is then loaded into a DataTable which becomes the data source for the data grid.
        /// The path to the bin folder is retrieved via the CodeBase property of the executing assembly. This property
        /// retruns the file system path as a URI. The protocol part is stripped from that URI before using it with methods
        /// on file system classes (as they won't accept URIs).
        /// </summary>
        /// <param name="table">DataTable into which the infomration is to be loaded</param>
        private void LoadAssemblyVersionInformation(DataTable table)
        {
            string path = Server.MapPath("~/bin");
            DirectoryInfo directoryInfo = new DirectoryInfo(path);

            foreach (FileInfo fileInfo in directoryInfo.GetFiles("*.dll"))
            {
                table.Rows.Add(new object[]
                                   {
                                       fileInfo.Name,
                                       FileVersionInfo.GetVersionInfo(fileInfo.FullName).FileVersion,
                                       fileInfo.LastWriteTime
                                   });
            }
        }

        /// <summary>
        /// Retrieve the version of the current database and load it into the speicified DataTable which becomes the
        /// data source for the data grid.
        /// </summary>
        /// <param name="table">DataTable into which the infomration is to be loaded</param>
        private void LoadDatabaseVersionInformation(DataTable table)
        {
            DataRow row = table.NewRow();
            row[AssemblyNameColumn] = "Database";
            row[AssemblyVersionColumn] = DataAccess.ExecuteScalar(DataAccess.ConnectionString, "GetDatabaseVersion");
            table.Rows.Add(row);

            //add server name 03-17-2009 jml29
            DataRow row1 = table.NewRow();
            row1[AssemblyNameColumn] = "Server";
            row1[AssemblyVersionColumn] = Environment.MachineName;
            row1[BuildTimestampColumn] = Environment.Version.ToString();
            table.Rows.Add(row1);
        }

        #endregion

        #region Web Form Designer generated code

        /// <summary>
        /// Initialize the page. The code for this method is generated by the Wizard and we have nothing else to add.
        /// </summary>
        /// <param name="e">Argument associated with the event</param>
        protected override void OnInit(EventArgs e)
        {
            //
            // CODEGEN: This call is required by the ASP.NET Web Form Designer.
            //
            InitializeComponent();
            base.OnInit(e);
        }

        /// <summary>
        /// Required method for Designer support - do not modify
        /// the contents of this method with the code editor.
        /// </summary>
        private void InitializeComponent()
        {
            this.Load += new System.EventHandler(this.OnPageLoad);
        }

        #endregion
    }
}